package com.intellagric.controller.upload;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.utils.FastDFSClient;
import com.intellagric.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: zhy
 * @Date: 2018/10/2
 * @Description: 文件上传接口
 */
@Controller
public class Upload {

    @Value("${IMAGE_SERVER_URL}")
    private String IMAGE_SERVER_URL;

    /**
     * 添加图片，通用接口
     * @Param multipartFile
     * @return String
    */
    @RequestMapping(value = "/upload/pic",produces = MediaType.TEXT_PLAIN_VALUE + ";charset=utf-8")
    @ResponseBody
    public String uploadPic(MultipartFile uploadFile) {

        try {
            //取文件的扩展名
            String originalFilename = uploadFile.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //创建一个FastDFS的客户端
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:conf/dfsclient.conf");
            //执行上传处理
            String path = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
            //拼接返回的url和ip地址，拼装成完整的url
            String url = IMAGE_SERVER_URL + '/' + path;
            //返回map
            Map result = new HashMap<>();
            result.put("code", 0);//符合layui前台框架上传接口返回值标准
            result.put("msg", "图片上传成功");
            result.put("url", url);
            //转成json字符串
            String json = JsonUtils.objectToJson(result);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            //5、返回map
            Map result = new HashMap<>();
            result.put("code", 1);
            result.put("msg", "图片上传失败");
            //转成json字符串
            String json = JsonUtils.objectToJson(result);
            return json;
        }
    }

    public String uploadPic(BufferedImage uploadFile, String extName) {
        try {

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            boolean flag = ImageIO.write(uploadFile, extName, out);
            byte[] b = out.toByteArray();

            //创建一个FastDFS的客户端
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:conf/dfsclient.conf");
            //执行上传处理
            String path = fastDFSClient.uploadFile(b, extName);
            //拼接返回的url和ip地址，拼装成完整的url
            String url = IMAGE_SERVER_URL + '/' + path;
            //返回map
            Map result = new HashMap<>();
            result.put("code", 0);//符合layui前台框架上传接口返回值标准
            result.put("msg", "图片上传成功");
            result.put("url", url);
            //转成json字符串
            String json = JsonUtils.objectToJson(result);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            //5、返回map
            Map result = new HashMap<>();
            result.put("code", 1);
            result.put("msg", "图片上传失败");
            //转成json字符串
            String json = JsonUtils.objectToJson(result);
            return json;
        }
    }

    /**
     * 添加图片，layedit富文本编辑器专用接口
     * @Param multipartFile
     * @return String
     */
    @RequestMapping(value = "/layedit/upload/pic",produces = MediaType.TEXT_PLAIN_VALUE + ";charset=utf-8")
    @ResponseBody
    public String uploadPicLayedit(MultipartFile file) {
        try {
            //取文件的扩展名
            String originalFilename = file.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //创建一个FastDFS的客户端
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:conf/dfsclient.conf");
            //执行上传处理
            String path = fastDFSClient.uploadFile(file.getBytes(), extName);
            //拼接返回的url和ip地址，拼装成完整的url
            String url = IMAGE_SERVER_URL + '/' + path;
            //返回map
            Map result = new HashMap<>();
            result.put("code", 0);//符合layui前台框架上传接口返回值标准
            result.put("msg", "图片上传成功");
            Map data = new HashMap<>();
            data.put("src",url);
            result.put("data", data);
            //转成json字符串
            String json = JsonUtils.objectToJson(result);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            //5、返回map
            Map result = new HashMap<>();
            result.put("code", 1);
            result.put("msg", "图片上传失败");
            //转成json字符串
            String json = JsonUtils.objectToJson(result);
            return json;
        }
    }

    /**
     * 编辑器图片上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload/editPicUpload")
    @ResponseBody
    public String editPicUpload(@RequestParam(value = "file") MultipartFile file){
        try {
            String filename=file.getOriginalFilename();
            String exName=filename.substring(filename.lastIndexOf('.')+1);
            FastDFSClient dfsClient = new FastDFSClient("classpath:conf/dfsclient.conf");

            String path=dfsClient.uploadFile(file.getBytes(),exName);

            String url=IMAGE_SERVER_URL+"/"+path;
            Map result=new HashMap();
            result.put("code",0);
            result.put("msg","上传成功");

            Map<String,String> m=new HashMap<String,String>();
            m.put("src",url);
            result.put("data",m);

            return JsonUtils.objectToJson(result);

        } catch (Exception e) {
            e.printStackTrace();
            LayuiDataGridResult result=new LayuiDataGridResult();
            result.setCode(1);
            result.setMsg("图片上传失败");
            result.setData(null);
            return JsonUtils.objectToJson(result);
        }
    }

}
